{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 复习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1<5>3>10)<(1<10>3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int((1.00+2.0)*7/2%22//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0 1.0\n"
     ]
    }
   ],
   "source": [
    "a = 3/3\n",
    "b = 3.0 // 3\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(7.0//3) #2.0 \n",
    "\n",
    "print(-7//3) #-3\n",
    "\n",
    "print(7//-3) #-3\n",
    "\n",
    "print(3//7) # 0\n",
    "\n",
    "print(-3//7) #-1\n",
    "\n",
    "print(3//-7) # -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = b = 1.0  \n",
    "c=1.0\n",
    "d=1.0\n",
    "print(a is b)\n",
    "print(c is d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True False\n"
     ]
    }
   ],
   "source": [
    "a = 1000 is 1000\n",
    "b = 1000 \n",
    "c = 1000\n",
    "d = b is c\n",
    "print(a,d) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = [1, 2, 4]\n",
    "id(a[1]) == id(b[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "list1 = {'1' : 1, '2' : 2}\n",
    "list2 = list1\n",
    "list1['1'] = 5\n",
    "sum = list1['1'] + list2['1']\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b=4,5\n",
    "print(a,b)\n",
    "a,b = (6,7)\n",
    "print(a,b)\n",
    "a,b=\"AB\"\n",
    "print(a,b)\n",
    "((a,b),c)=('AB','CD')\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取下列两个列表中共有的元素，以列表形式返回\n",
    "l1 = [1, 2, 3, 5, 6, 7]\n",
    "l2 = [1, 2, 3, 9, 0, 0]\n",
    "s1 = set(l1)\n",
    "s2 = set(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(s1 & s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = [1, 2, 3, 5, 6, 7]\n",
    "l2 = [1, 2, 3, 9, 0, 0]\n",
    "l3 = []\n",
    "for i in range(6):\n",
    "    if l1[i] in l2 and l1[i] not in l3:\n",
    "        l3.append(l1[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 15, 31]\n"
     ]
    }
   ],
   "source": [
    "#获取下列3个列表中共有的元素，并有小到大排序\n",
    "l0 = [27,16,4,6,31,5,15,21,17,7]\n",
    "l1 = [24,19,27,16,4,6,31,5,15,45,35,1,2,3]\n",
    "l2 = [31,29,21,17,5,7,30,13,22,2,3,4,19,15,11]\n",
    "l3 = list(set(l0) & set(l1) & set(l2))\n",
    "l3.sort()\n",
    "print(l3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 已知三引号创建的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "story = '''\n",
    "the wolf swallowed a bone by mistake, which was very uncomfortable.\n",
    "he ran around looking for a doctor.\n",
    "he met the Osprey and asked him to take out the bone.\n",
    "the Osprey put her head into the wolf's throat and took out the bone, so she asked the wolf for a good reward.\n",
    "the wolf replied, \"Hey, friend, you can take it back safely from the wolf's mouth.\n",
    "isn't it satisfied? How can you pay?\"'''\n",
    "# str.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = story.split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The wolf swallowed a bone by mistake, which was very uncomfortable.He ran around looking for a doctor.He met the osprey and asked him to take out the bone.The osprey put her head into the wolf's throat and took out the bone, so she asked the wolf for a good reward.The wolf replied, \"hey, friend, you can take it back safely from the wolf's mouth.Isn't it satisfied? how can you pay?\"\n"
     ]
    }
   ],
   "source": [
    "lNew = []\n",
    "for i in l:\n",
    "    lNew.append(i.capitalize())\n",
    "print(''.join(lNew))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实现每句首字母大写，并将字符串内的换行取消，实现下图效果转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小易喜欢的字符串具有以下特性：\n",
    "1. 字符串每个字母都是大写字母\n",
    "2. 字符串没有连续相等的字母\n",
    "3. 字符串正着读和反着读一样\n",
    "\n",
    "输入一个字符串，判断小易是否喜欢该字符串，喜欢则输出like，不喜欢就输出dislike"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "建立一个名为 life 的多级字典。将下面这些字符串作为顶级键：'animals'、'plants' 以及 'others'。令 'animals' 键指向另一个字典，这个字典包含键 'cats'、'octopi' 以及 'emus'。令 'cat' 键指向一个字符串列表，这个列表包括 'Henri'、'Grumpy' 和 'Lucy'。让其余的键都指向空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values0 = [ 'Henri', 'Grumpy' , 'Lucy']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values = {'cats':values0, 'octopi':{}, 'emus':{}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'animals':values, 'plants':{}, 'others':{}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {\n",
    "    \"animals\":{\n",
    "        'cats':[ 'Henri', 'Grumpy' , 'Lucy'],\n",
    "        'octopi':{},\n",
    "        'emus':{}\n",
    "    },\n",
    "    'plants':{},\n",
    "    'others':{}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串与字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 用户输入一个数字，打印出每一位数字以及他们出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "787777\n",
      "7\n",
      "8\n",
      "7\n",
      "7\n",
      "7\n",
      "7\n",
      "{'7': 5, '8': 1}\n"
     ]
    }
   ],
   "source": [
    "n = input()\n",
    "dic = {}\n",
    "for i in range(len(n)):\n",
    "    print(n[i])\n",
    "    if n[i] not in dic:\n",
    "        dic[n[i]] = 1\n",
    "    else:\n",
    "        dic[n[i]] +=1\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表与字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 将列表[1,3,2,5,4,1,2,3,6,8,4,2]中的重复元素去掉并排好序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 8}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set([1,3,2,5,4,1,2,3,6,8,4,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组和字典转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 已知两个元组对象，人名和成绩\n",
    "classmates = ('张一','王二','李三','赵四')\n",
    "\n",
    "score = (95,87,32,96)\n",
    "\n",
    "将人名和成绩一一对应合成一个字典对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张一': 95, '王二': 87, '李三': 32, '赵四': 96}\n"
     ]
    }
   ],
   "source": [
    "classmates = ('张一','王二','李三','赵四')\n",
    "score = (95,87,32,96)\n",
    "dic = {}\n",
    "for i in range(4):\n",
    "    dic[classmates[i]] = score[i]\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 存在一个列表list = [{'id' : 1,'name' : 'zhangsan','age' : '1'},{'id' : 2,'name' : 'lisi','age' : '2'}],请设计实现：用户输入姓名，返回对应年龄,如果姓名不在内，则输出Not Found。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lisi\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "list1 = [{'id' : 1,'name' : 'zhangsan','age' : '1'},{'id' : 2,'name' : 'lisi','age' : '2'}]\n",
    "str1 = input()\n",
    "flag = 0\n",
    "for i in range(2):\n",
    "    if list1[i]['name'] == str1:\n",
    "        flag =1\n",
    "        print(list1[i]['age'])\n",
    "if flag == 0:\n",
    "    print('Not Found')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 浅拷贝与深拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [100]\n",
    "b = a\n",
    "b[0] = 0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100]\n"
     ]
    }
   ],
   "source": [
    "a = [100,1,2,3]\n",
    "b = a[:]\n",
    "b[0] = 0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,[4,5,6]]\n",
    "b = a[:]\n",
    "b[0] = 0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [0, 5, 6]]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,[4,5,6]]\n",
    "b = a[:]\n",
    "b[-1][0] = 0\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [0, 5, 6]]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,[4,5,6]]\n",
    "b = a\n",
    "b[-1][0] = 0\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,[4,5,6]]\n",
    "b = a.copy()\n",
    "b[-1][0] = 0\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, [4, 5, 6]]\n",
      "[1, 2, 3, [66, 5, 6]]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "a = [1,2,3,[4,5,6]]\n",
    "b = copy.deepcopy(a)\n",
    "b[-1][0] = 66\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 浅拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将对象中第一级子对象的引用进行拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 深拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将对象中所有层级的子对象的引用进行拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**总结：**\n",
    "\n",
    "1. 浅拷贝与深拷贝都是在拷贝引用地址而不拷贝值，区别在于拷贝的层级不一样\n",
    "2. Python中，一切都是对象\n",
    "3. \"=\" 就是复制对象的引用"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
